//
//  UICustomTabBar.m
//  EuroTripValenciaGuide
//
//  Created by Dario Trisciuoglio on 22/07/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "UICustomTabBar.h"


@implementation UICustomTabBar

@synthesize btn1, btn2, btn3, btn4;

- (void)viewDidAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	
	[self hideTabBar];
	[self addCustomElements];
}

- (void)hideTabBar
{
	for(UIView *view in self.view.subviews)
	{
		if([view isKindOfClass:[UITabBar class]])
		{
			view.hidden = YES;
			break;
		}
	}
}

- (void)hideNewTabBar 
{
    self.btn1.hidden = 1;
    self.btn2.hidden = 1;
    self.btn3.hidden = 1;
    self.btn4.hidden = 1;
}

- (void)showNewTabBar 
{
    self.btn1.hidden = 0;
    self.btn2.hidden = 0;
    self.btn3.hidden = 0;
    self.btn4.hidden = 0;
}

-(void)addCustomElements
{
	// Inizializza le nostre due immagini
	UIImage *btnImage = [UIImage imageNamed:@"home.png"];
	UIImage *btnImageSelected = [UIImage imageNamed:@"home_hover.png"];
	
	self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; //Installo il bottone
	btn1.frame = CGRectMake(0, 430, 80, 50); // Imposto le caratteristiche grafiche del bottone (dimensione e posizione) 
	[btn1 setBackgroundImage:btnImage forState:UIControlStateNormal]; // Imposto l'immagine del bottone per lo stato normale 
	[btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; // Imposto l'immagine del bottone per lo stato selezionato 
	[btn1 setTag:0]; // Assegno un'etichetta al bottone così quando il bottone viene premuto, possiamo riconosciere quale bottone è stato premuto.
	[btn1 setSelected:true]; // Imposto questo bottone come selezionato poiche è il pulsante dell'home.
	
	// Ora ripetiamo il processo per gli altri bottoni
	btnImage = [UIImage imageNamed:@"mappa.png"];
	btnImageSelected = [UIImage imageNamed:@"mappa_hover.png"];
	self.btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
	btn2.frame = CGRectMake(80, 430, 80, 50);
	[btn2 setBackgroundImage:btnImage forState:UIControlStateNormal];
	[btn2 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
	[btn2 setTag:1];
	
	btnImage = [UIImage imageNamed:@"eventi.png"];
	btnImageSelected = [UIImage imageNamed:@"eventi_hover.png"];
	self.btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
	btn3.frame = CGRectMake(160, 430, 80, 50);
	[btn3 setBackgroundImage:btnImage forState:UIControlStateNormal];
	[btn3 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
	[btn3 setTag:2];
	
	btnImage = [UIImage imageNamed:@"altro.png"];
	btnImageSelected = [UIImage imageNamed:@"altro_hover.png"];
	self.btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
	btn4.frame = CGRectMake(240, 430, 80, 50);
	[btn4 setBackgroundImage:btnImage forState:UIControlStateNormal];
	[btn4 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
	[btn4 setTag:3];
	
	// Aggiungo i miei nuovi bottoni alla vista
	[self.view addSubview:btn1];
	[self.view addSubview:btn2];
	[self.view addSubview:btn3];
	[self.view addSubview:btn4];
	
	// Installo gli operatori dell'evento in modo che il metodo buttonClicked risponda al click.
	[btn1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
	[btn2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
	[btn3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
	[btn4 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonClicked:(id)sender
{
	int tagNum = [sender tag];
	[self selectTab:tagNum];
}

- (void)selectTab:(int)tabID
{
	switch(tabID)
	{
		case 0:
			[btn1 setSelected:true];
			[btn2 setSelected:false];
			[btn3 setSelected:false];
			[btn4 setSelected:false];
			break;
		case 1:
			[btn1 setSelected:false];
			[btn2 setSelected:true];
			[btn3 setSelected:false];
			[btn4 setSelected:false];
			break;
		case 2:
			[btn1 setSelected:false];
			[btn2 setSelected:false];
			[btn3 setSelected:true];
			[btn4 setSelected:false];
			break;
		case 3:
			[btn1 setSelected:false];
			[btn2 setSelected:false];
			[btn3 setSelected:false];
			[btn4 setSelected:true];
			break;
	}	
	
	self.selectedIndex = tabID;
	
	
}

- (void)dealloc {
	[btn1 release];
	[btn2 release];
	[btn3 release];
	[btn4 release];
	
    [super dealloc];
}

@end
